#include "FloatingTextManager.h"

FloatingTextManager::FloatingTextManager(void)
{
}

FloatingTextManager::~FloatingTextManager(void)
{
}

FloatingTextManager::FloatingTextManager(const FloatingTextManager &mm)
{
}

FloatingTextManager* FloatingTextManager::operator=(const FloatingTextManager &mm)
{
	return this;
}

FloatingTextManager* FloatingTextManager::Instance()
{
	static FloatingTextManager instance;

	return &instance;
}

void FloatingTextManager::Initialize(ID3DXSprite *sprite)
{
	mSprite = sprite;
}


void FloatingTextManager::Update(float dt)
{
	Texts::iterator t = textData.begin();
	while (t != textData.end())
	{
		(*t)->lifeTime -= dt;
		if ((*t)->lifeTime < 0.0f)
		{
			delete [16] (*t)->text;
			delete *t;
			t = textData.erase(t);
		}
		else
		{
			if ((*t)->lifeTime > (*t)->maxLifeTime / 2)
			{
				(*t)->scale += (((*t)->maxScale - (*t)->minScale) / ((*t)->maxLifeTime)) * dt;
			}
			else
			{
				(*t)->scale -= (((*t)->maxScale - (*t)->minScale) / ((*t)->maxLifeTime)) * dt;
			}

			t++;
		}
	}
}


void FloatingTextManager::Render()
{
	mSprite->Begin(D3DXSPRITE_ALPHABLEND);
	// reset the sprite's matrix
	
	Texts::iterator t = textData.begin();
	while (t != textData.end())
	{
		D3DXMATRIX S, T;
		float s = (*t)->scale;
		D3DXMatrixScaling(&S, s, s, 1.0f);
		D3DXMatrixTranslation(&T, (*t)->position.x, (*t)->position.y, 0.0f);
		mSprite->SetTransform(&(S*T));
		RECT R = {0, 0, 0, 0};
		(*t)->font->DrawTextA(mSprite, (*t)->text, -1, &R, DT_NOCLIP | DT_CENTER | DT_VCENTER, (*t)->color);
		t++;
	}

	mSprite->End();
}

void FloatingTextManager::AddFloatingText(ID3DXFont *font, char* text, float lifeTime, D3DXVECTOR3 position, float minScale, float maxScale, D3DCOLOR color)
{
	TextData * td = new TextData;
	td->font = font;
	td->maxLifeTime = lifeTime;
	td->lifeTime = lifeTime;
	td->position = position;
	td->minScale = minScale;
	td->maxScale = maxScale;
	td->scale = minScale;
	char *buffer = new char[16];
	memcpy(buffer, text, 16);
	td->text = buffer;
	td->color = color;

	textData.push_back(td);
}

void FloatingTextManager::AddFloatingText(ID3DXFont *font, int num, float lifeTime, D3DXVECTOR3 position, float minScale, float maxScale, D3DCOLOR color)
{
	TextData * td = new TextData;
	td->font = font;
	td->maxLifeTime = lifeTime;
	td->lifeTime = lifeTime;
	td->position = position;
	td->minScale = minScale;
	td->maxScale = maxScale;
	td->scale = minScale;
	char *buffer = new char[16];
	sprintf_s(buffer, 16, "%i\n", num);
	td->text = buffer;
	td->color = color;

	textData.push_back(td);
}

void FloatingTextManager::AddFloatingText(ID3DXFont *font, float num, float lifeTime, D3DXVECTOR3 position, float minScale, float maxScale, D3DCOLOR color)
{
	TextData * td = new TextData;
	td->font = font;
	td->maxLifeTime = lifeTime;
	td->lifeTime = lifeTime;
	td->position = position;
	td->minScale = minScale;
	td->maxScale = maxScale;
	td->scale = minScale;
	char *buffer = new char[16];
	sprintf_s(buffer, 16, "%.0f\n", num);
	td->text = buffer;
	td->color = color;

	textData.push_back(td);
}
